package com.qfedu.buy.server.service.impl;

import cn.hutool.core.util.StrUtil;
import com.qfedu.buy.common.config.SystemConfig;
import com.qfedu.buy.common.util.AliOssUtil;
import com.qfedu.buy.common.util.FileUtil;
import com.qfedu.buy.server.entity.Ossstorage;
import com.qfedu.buy.server.dao.OssstorageDao;
import com.qfedu.buy.server.service.intf.OssstorageService;
import com.qfedu.buy.server.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @program: NeighborBuy
 * @description: 邻里购真正意义上最懂你的新零售平台
 * @author: Feri(老邢)
 * @create: 2021-01-26 14:22:26
 */
@Service
public class OssstorageServiceImpl implements OssstorageService{
    /*
    *注入持久层对象*/
    @Autowired
    private OssstorageDao dao;
    /*
    * 新增内容*/
    @Override
    public R save(Ossstorage ossstorage) {
        if(ossstorage!=null){
            if(dao.insert(ossstorage)>0){
                return R.ok();
            }
        }
        return R.fail("新增异常");
    }
    /*
    * 查询 所有数据*/
    @Override
    public R queryAll() {
        return R.ok(dao.selectAll());
    }

    @Override
    public R upload(MultipartFile file) {
        if(file!=null && !file.isEmpty()){
            String objname= FileUtil.renameFile(file.getOriginalFilename());
            try {
                String url= AliOssUtil.upload(SystemConfig.OSS_BNAME,objname,file.getBytes());
                if(!StrUtil.isEmptyIfStr(url)){
                    Ossstorage ossstorage=new Ossstorage(SystemConfig.OSS_BNAME,SystemConfig.OSS_TYPE1,objname,url);
                    dao.insert(ossstorage);
                    return R.ok(ossstorage.getId());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail("亲，网络异常，上传失败！");
    }
}